part of YFree;

class _YFreeCommonPlugin {
  final _secureStorage = FlutterSecureStorage(
    aOptions: AndroidOptions(
      encryptedSharedPreferences: true,
      preferencesKeyPrefix: "YFree_",
      sharedPreferencesName: "YFree",
    ),
  );

  void doCrash(String message) {
    if (YConfig.catchCrash_jkid.isEmpty) return;
    yPages.first.yPost(YConfig.catchCrash_jkid, {"msg": message}, (isSuccess, results, code, message) {});
  }

  void checkUpdate({
    required int newVersion,
    required String updateContent,
    required String? apkDownloadPath,
    required String? androidAppId,
    required String? iOSAppId,
    bool enableIgnore = false,
    onNewest()?,
  }) {}

  void finishApplication() {
    exit(0);
  }

  void showProgress() {
    yIsShowProgress = true;
    BotToast.showLoading();
  }

  void dismissProgress() {
    yIsShowProgress = false;
    BotToast.closeAllLoading();
  }

  void log(String message) {
    if (!YConfig.DEBUG) return;
    print("YFreeLog：" + message);
  }

  void toast(String message) {
    BotToast.showText(text: message);
  }

  void configIP(String ip) {}

  void configUpdateIP(String updateIp) {}

  Future<void> runOnUIThread(int delay, action()) {
    return Future.delayed(Duration(milliseconds: delay), () => action());
  }

  String getAppName() {
    return YConfig.appName;
  }

  String base64_Encode(String content) {
    return base64Encode(utf8.encode(content));
  }

  String base64_Decode(String content) {
    return utf8.decode(base64Decode(content));
  }

  String aesEncode(String content, String password) {
    final key = enc.Key.fromUtf8(password);
    final iv = enc.IV.fromUtf8("0000000000000000");
    final encrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.cbc));
    final encrypted = encrypter.encrypt(content, iv: iv);
    return encrypted.base64;
  }

  String aesDecode(String content, String password) {
    final key = enc.Key.fromUtf8(password);
    final iv = enc.IV.fromUtf8("0000000000000000");
    final encrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.cbc));
    final decrypted = encrypter.decrypt64(content, iv: iv);
    return decrypted;
  }

  Future<String> getDeviceID() async {
    var deviceID = await yRead("YFree_DeviceID", secure: true);
    if (deviceID != null) return deviceID;
    deviceID = await PlatformDeviceId.getDeviceId;
    ySave("YFree_DeviceID", deviceID!, secure: true);
    return deviceID;
  }

  Future<bool> requestStorage() {
    return Future.value(true);
  }

  Future<bool> isNetAble() async {
    final result = (await Connectivity().checkConnectivity());
    return result != ConnectivityResult.none;
  }

  Future<void> save(String key, String value, {bool secure = false}) async {
    if (secure) {
      await _secureStorage.write(key: key, value: value);
    } else {
      await (await SharedPreferences.getInstance()).setString(key, value);
    }
  }

  Future<String?> read(String key, {bool secure = false}) async {
    if (secure) {
      return _secureStorage.read(key: key);
    } else {
      return (await SharedPreferences.getInstance()).getString(key);
    }
  }

  Future<void> delete(String key, {bool secure = false}) async {
    if (secure) {
      await _secureStorage.delete(key: key);
    } else {
      await (await SharedPreferences.getInstance()).remove(key);
    }
  }

  void copyClipBoard(String content) {
    Clipboard.setData(ClipboardData(text: content));
  }
}
